package com.rongke.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.rongke.commons.JsonResp;
import com.rongke.model.Question;
import com.rongke.model.QuestionOption;
import com.rongke.service.QuestionOptionService;
import com.rongke.service.QuestionService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @QuestionController
 * @Controller
 * @version : Ver 1.0
 */
@RestController
@RequestMapping(value="/api/question")
@Transactional
@CrossOrigin
public class QuestionController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private QuestionService questionService;
    @Autowired
    private QuestionOptionService questionOptionService;

    /**
     * @添加
     * @param question
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/add", method = RequestMethod.POST)
    public JsonResp addQuestion(@RequestBody Question question){
        log.debug("添加");
        questionService.insert(question);
        return JsonResp.ok(question);
    }

    /**
     * @修改
     * @param question
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/update", method = RequestMethod.POST)
    public JsonResp updateQuestion(@RequestBody Question question){
        log.debug("修改");
        questionService.updateById(question);
        return JsonResp.ok(question);
    }
    /**
     * @根据id查找
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/selectOne", method = RequestMethod.GET)
    public JsonResp selectQuestion(Long id){
        log.debug("查找");
        Question question = questionService.selectById(id);
        return JsonResp.ok(question);
    }

    /**
     * @根据条件查找
     * @param question
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/selectByCondition", method = RequestMethod.POST)
    public JsonResp selectQuestionByCondition(@RequestBody Question question){
        log.debug("根据条件查找");
        if(question==null)
            return JsonResp.toFail("参数不能为空");
        List<Question> questionList=questionService.selectByCondition(question);
        return JsonResp.ok(questionList);
    }
    /**
     * @根据id删除问题
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/del", method = RequestMethod.GET)
    public JsonResp del(Long id){
        log.debug("删除 问题 选项");
        questionService.selectById(id);
        EntityWrapper<QuestionOption> qo = new EntityWrapper<>();
        qo.eq("question_id",id.toString());
        questionOptionService.delete(qo);
        return JsonResp.ok();
    }

    /**
     * @修改选项
     * @param questionOption
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/updateOption", method = RequestMethod.POST)
    public JsonResp updateOption(@RequestBody QuestionOption questionOption){
        log.debug("修改");
        questionOptionService.updateById(questionOption);
        return JsonResp.ok(questionOption);
    }

    /**
     * @根据id删除选项
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/delOption", method = RequestMethod.GET)
    public JsonResp delOption(Long id){
        log.debug("删除选项");
        questionOptionService.deleteById(id);
        return JsonResp.ok();
    }
}
